<?php
	$con = mysql_connect("localhost:3306","root","shibani");
	if (!$con)
	{
		die('Could not connect: ' . mysql_error());
	}

	mysql_select_db("cal2sync", $con);

	$result = mysql_query("SELECT u_id FROM user WHERE username = '".$_POST['username']."'");
	while($row = mysql_fetch_array($result))
	{
		$uid = $row['u_id'];
	}
	
	$res = mysql_query("SELECT action FROM rules WHERE u_id = '".$uid."' AND event_type = '".$_POST['event_type']."'");
	$res_num = mysql_num_rows($res);
	
	if($res_num==0)  // if rule does not exist for this event insert new rule
	{
		mysql_query("INSERT INTO rules (u_id, event_type, action)VALUES ('".$uid."','".$_POST['event_type']."','".$_POST['action']."')");
	}
	else     // rule already exists for this event update the row
	{
		mysql_query("UPDATE rules SET action = '".$_POST['action']."' WHERE u_id = '".$uid."' AND event_type = '".$_POST['event_type']."'");
	}
	mysql_close($con);
	
	//echo "Done inserting rules";
?>